package com.gm.wholesale.module.setting.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.gm.commonlib.base.baseui.baseFragment.BaseFragment;
import com.gm.commonlib.views.dialog.loadingDialog.LoadingDialog;
import com.gm.commonlib.views.toast.ToastUtil;
import com.gm.wholesale.bluetooth.BluetoothUtil;
import com.gm.wholesale.bluetooth.DeviceManger;
import com.gm.wholesale.bluetooth.bean.DeviceBean;
import com.gm.wholesale.bluetooth.bean.WeightBean;
import com.gm.wholesale.bluetooth.interfaces.OnConnectionBluListener;
import com.gm.wholesale.bluetooth.printBlue.ReceiptPrintBluethManger;
import com.gm.wholesale.constant.Constant;
import com.gm.wholesale.module.setting.view.BluetoothListView;
import com.gm.wholesale.util.ViewUtil;

import java.util.ArrayList;
import java.util.List;

public class ReceiptBlueListFragment extends BaseFragment {
    private BluetoothListView mPrintBlueDeviceView;
    private ReceiptPrintBluethManger mReceiptPrintBluethManger;
    private boolean mIsToConnection;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mPrintBlueDeviceView = null;
        mPrintBlueDeviceView = new BluetoothListView(getActivity());
        showPrintBlueDevices();
        return mPrintBlueDeviceView;

    }

    private void showPrintBlueDevices() {
        mReceiptPrintBluethManger = ReceiptPrintBluethManger.newInstance(getContext());
        List<String> filter=new ArrayList<>();
        filter.add(Constant.RECEIT_PRINT);
        mPrintBlueDeviceView.showDevice(filter, getPrintConDevice());
        mPrintBlueDeviceView.setOnItemClickListener(deviceBean -> {
            if (deviceBean.getState() == 1) {
                mIsToConnection = false;
                ToastUtil.showToast("已断开连接");
                mPrintBlueDeviceView.notifyDataSetChanged("");
                DeviceManger.getDeviceManger().cleanReceiPrintDevice();
            } else {
                mIsToConnection = true;
                mReceiptPrintBluethManger.setOnConnectionListener(new OnConnectionBluListener() {
                    @Override
                    public void connection(DeviceBean deviceBean1) {
                        mIsToConnection = false;
                        if (deviceBean1!=null){
                            DeviceManger.getDeviceManger().saveReceiPrintDevice(deviceBean1);
                        }
                        LoadingDialog.newLoadingDialog().hid();
                        ToastUtil.showToast("连接成功");
                        mPrintBlueDeviceView.notifyDataSetChanged(deviceBean.getAddress());
                    }

                    @Override
                    public void connectionFail() {
                        if (mIsToConnection) {
                            ViewUtil.showPrintError();
                        }
                        LoadingDialog.newLoadingDialog().hid();
                    }

                    @Override
                    public void getNumber(WeightBean weightBean) {

                    }
                });
                mReceiptPrintBluethManger.connectByDevice(getContext(), deviceBean.getAddress());
            }

        });


    }

    private String getPrintConDevice() {
        String connectionDevice = null;
        if (mReceiptPrintBluethManger.getConnectionState() == 3) {
            connectionDevice = mReceiptPrintBluethManger.getConnectionDevice();
        }
        return connectionDevice;
    }

    @Override
    public void onDestroy() {
        if (mPrintBlueDeviceView != null) {
            mPrintBlueDeviceView.stop();
        }
        super.onDestroy();
    }


}
